package com.cxs.utils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BasePageBean<T> {
    private Integer pageNum;
    private Integer pageSize;
    private Long count;
    private Integer startRow;
    private Integer pages;
    private List<T> data;

    public BasePageBean(Integer pageNum, Integer pageSize, Integer count){
        if (count == null || pageSize == null) {
            throw new RuntimeException("分页参数不能为空");
        }
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.count = Long.parseLong(count + "");

        if (count < 0) {
            this.count = 0L;
        }
        if (pageSize < 0) {
            this.pageSize = 10;
        }
        if (pageNum <= 0) {
            this.pageNum = 1;
        }

        if (this.count % this.pageSize == 0) {
            this.pages = Integer.parseInt(this.count.toString()) / this.pageSize;
        } else {
            this.pages = Integer.parseInt(this.count.toString()) / this.pageSize + 1;
        }

        this.startRow = (this.pageNum - 1) * this.pageSize;
    }
}
